//-----------------------------------------------------------------------
// <copyright file="Mailbox.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Microsoft.Live.Messenger
{
    using System;
    using System.ComponentModel;
    using Microsoft.Live.Core;

    /// <summary>
    /// Represents the User's Windows Live Hotmail mailbox.
    /// </summary>
    public class Mailbox : INotifyPropertyChanged
    {
        private readonly User user;

        private MessengerEntity entity;

        /// <summary>
        /// Initializes a new instance of the <see cref="Mailbox"/> class.
        /// </summary>
        /// <param name="User">The associated User.</param>
        public Mailbox(User user)
        {
            if (user == null)
            {
                throw ExceptionHelper.CreateArgumentNullException("User");
            }

            this.user = user;
        }

        internal void Initialize()
        {
            this.entity = new MessengerEntity(this.user.Entity.GetProperty("Mailbox"));
            this.entity.AttachEvent("EmailReceived", this.OnEmailReceived);
            this.entity.AttachEvent("PropertyChanged", this.OnPropertyChanged);
        }

        /// <summary>
        /// Gets the initial mail count of the inbox.
        /// </summary>
        public int InboxInitialCount
        {
            get { return this.entity.GetNumberProperty<int>("InboxInitialCount"); }
        }

        /// <summary>
        /// Gets the current unread mail count of the inbox.
        /// </summary>
        public int InboxUnreadCount
        {
            get { return this.entity.GetNumberProperty<int>("InboxUnreadCount"); }
        }

        /// <summary>
        /// Gets the initial mail count for all folders.
        /// </summary>
        public int OtherInitialCount
        {
            get { return this.entity.GetNumberProperty<int>("OtherInitialCount"); }
        }

        /// <summary>
        /// Gets the current unread mail count for all folders.
        /// </summary>
        public int OtherUnreadCount
        {
            get { return this.entity.GetNumberProperty<int>("OtherUnreadCount"); }
        }

        /// <summary>
        /// Occurs when the User receives an email.
        /// </summary>
        public event EmailReceivedEventHandler EmailReceived;

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void OnEmailReceived(object sender, MessengerEventArgs e)
        {
            MessengerEntity messageUrl = new MessengerEntity(e.Entity.GetProperty("MessageUrl"));

            this.OnEmailReceived(
                new EmailReceivedEventArgs(
                    e.Entity.GetProperty<string>("Subject"),
                    e.Entity.GetProperty<string>("Sender"),
                    e.Entity.GetProperty<string>("SenderAddress"),
                    e.Entity.GetUriProperty("MessageUrl"),
                    e.Entity.GetEnumProperty<MailFolder>("MailFolder")));
        }

        /// <summary>
        /// Raises the <see cref="EmailReceived"/> event.
        /// </summary>
        /// <param name="e">An <see cref="EmailReceivedEventArgs"/> that contains the event data.</param>
        protected virtual void OnEmailReceived(EmailReceivedEventArgs e)
        {
            if (this.EmailReceived != null)
            {
                this.EmailReceived(this, e);
            }
        }

        private void OnPropertyChanged(object sender, MessengerEventArgs e)
        {
            this.OnPropertyChanged(
                new PropertyChangedEventArgs(
                    e.Entity.GetProperty<string>("PropertyName")));
        }

        /// <summary>
        /// Raises the <see cref="PropertyChanged"/> event.
        /// </summary>
        /// <param name="e">A <see cref="PropertyChangedEventArgs"/> that contains the event data.</param>
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, e);
            }
        }
    }
}
